package com.jorado.etl.service;

import com.jorado.basedata.BaseDataUtils;
import com.jorado.core.Result;
import com.jorado.etl.model.EtlInput;
import com.jorado.etl.repository.ESPositionRepository;
import com.jorado.event.EventClient;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import org.springframework.context.annotation.Description;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

@Description("Es fasttext语料抽取服务")
public final class EsFastTextEtlService extends ExportToFileImpl<Map<String, Object>> {

    private int total;
    private int waitTime;
    private FastTextEtlService etlService;

    public EsFastTextEtlService(String businessKey, String distFile) {
        this(businessKey, distFile, true);
    }

    public EsFastTextEtlService(String businessKey, String distFile, boolean deleteIfExists) {
        this(businessKey, distFile, deleteIfExists, 0);
    }

    public EsFastTextEtlService(String businessKey, String distFile, boolean deleteIfExists, int waitTime) {
        super(new ESPositionRepository(businessKey), distFile, deleteIfExists);
        this.waitTime = waitTime;
        this.etlService = new FastTextEtlService(this.distFile);
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<Map<String, Object>> dataList) {

        total += dataList.size();

        HashSet<EtlInput> etlList = new LinkedHashSet<>();

        String jobTypeId;
        String jobTitle;
        String jobType;
        String jobDescription;

        for (Map<String, Object> doc : dataList) {

            jobTypeId = doc.get("job_type").toString();

            jobType = BaseDataUtils.getSubJobTypeName(jobTypeId);

            jobTitle = doc.get("job_title").toString();

            if (jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
                continue;

            jobDescription = doc.get("job_desc").toString();

            EtlInput etlInput = new EtlInput();

            etlInput.setJobTypeId(jobTypeId);
            etlInput.setJobType(jobType);
            etlInput.setJobTitle(jobTitle);
            etlInput.setJobDescription(jobDescription);

            etlList.add(etlInput);

        }

        this.etlService.save(etlList);

        try {
            Thread.sleep(waitTime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取职位:[%d]个", total));

        //TODO:对文件去重
        return Result.OK;
    }
}
